package server

import (
	"360-cinema/user-server/basic/config"
	"360-cinema/user-server/basic/inits"
	"360-cinema/video-server/basic/handler/model"
	__ "360-cinema/video-server/basic/proto"
	"context"
	"encoding/json"
	"gorm.io/gorm"
	"time"
)

type Server struct {
	__.UnimplementedVideoServer
}

func (s Server) VideoRd(_ context.Context, in *__.VideoRdReq) (*__.VideoRdResp, error) {

	var Video model.Video
	rd := (float64(Video.Play) * 0.6) + (float64(Video.Like) * 0.2) + (float64(Video.Collect) * 0.1) + (float64(Video.Comment) * 0.1)
	Video = model.Video{
		Model: gorm.Model{
			ID: uint(in.VideoId),
		},
		Rd: int(rd),
	}
	err := model.UpdateVideo(&Video, int(in.VideoId))
	if err != nil {
		return &__.VideoRdResp{
			Code: 0,
			Msg:  "计算失败",
		}, nil
	}
	return &__.VideoRdResp{
		Code: 200,
		Msg:  "计算成功",
		Data: int64(Video.ID),
	}, nil
}

func (s Server) ListHistory(_ context.Context, in *__.ListHistoryReq) (*__.ListHistoryResp, error) {

	var history model.History
	err := config.DB.Where("id=?", in.UserId).Find(&history).Error
	if err != nil {
		return nil, err
	}
	return &__.ListHistoryResp{
		UserId:   int64(history.UserId),
		VideoId:  int64(history.VideoId),
		DramaId:  int64(history.DramaId),
		Progress: int64(history.Progress),
	}, nil
}

func (s Server) CreateHistory(_ context.Context, in *__.CreateHistoryReq) (*__.CreateHistoryResp, error) {

	var history model.History
	history = model.History{
		UserId:   int(in.UserId),
		VideoId:  int(in.VideoId),
		DramaId:  int(in.DramaId),
		Progress: int(in.Progress),
	}
	err := model.CreateHistory(&history)
	if err != nil {
		return &__.CreateHistoryResp{
			Code: 0,
			Msg:  "添加失败",
		}, nil
	}
	return &__.CreateHistoryResp{
		Code: 200,
		Msg:  "添加成功",
		Data: int64(history.ID),
	}, nil

}

func (s Server) ListDrama(_ context.Context, in *__.ListDramaReq) (*__.ListDramaResp, error) {

	var drama model.Drama
	err := config.DB.Where("id=?", in.VideoId).Find(&drama).Error
	if err != nil {
		return nil, err
	}
	return &__.ListDramaResp{
		VideoId:  int64(drama.VideoId),
		Num:      int64(drama.Num),
		Title:    drama.Title,
		Duration: int64(drama.Duration),
		FileUrl:  drama.FileUrl,
		PlayTime: int64(drama.PlayTime),
	}, nil
}

func (s Server) VideoShow(_ context.Context, in *__.VideoShowReq) (*__.VideoShowResp, error) {
	//TODO implement me

	var video model.Video
	err := config.DB.Where("id=?", in.VideoId).First(&video).Error
	if err != nil {
		return nil, err
	}
	return &__.VideoShowResp{
		UserId:      int64(video.UserId),
		Title:       video.Title,
		Description: video.Description,
		Play:        int64(video.Play),
		Actor:       video.Actor,
		Director:    video.Director,
	}, nil
}

func (s Server) VideoList(_ context.Context, in *__.VideoListReq) (*__.VideoListResp, error) {
	//TODO implement me
	var video model.Video
	offset := (in.Page - 1) * in.Size
	err := config.DB.Offset(int(offset)).Limit(int(in.Size)).Find(&video).Error
	if err != nil {
		return nil, err
	}
	marshal, err := json.Marshal(&video)
	if err != nil {
		return nil, err
	}
	inits.Rdb.Set(inits.Ctx, "video", marshal, time.Minute*3)
	return &__.VideoListResp{
		UserId:      int64(video.UserId),
		Title:       video.Title,
		Description: video.Description,
		CoverUrl:    video.CoverUrl,
		FileUrl:     video.FileUrl,
		Type:        video.Type,
		Address:     video.Address,
		Year:        video.Year,
		Play:        int64(video.Play),
		Like:        int64(video.Like),
		Collect:     int64(video.Collect),
		Comment:     int64(video.Comment),
		Status:      video.Status,
		Reject:      video.Reject,
	}, nil
}

func (s Server) ListVideo(_ context.Context, in *__.ListVideoReq) (*__.ListVideoResp, error) {
	//TODO implement me

	var video model.Video
	err := config.DB.Where("type=?", in.Type).Find(&video).Error
	if err != nil {
		return nil, err
	}
	marshal, err := json.Marshal(&video)
	if err != nil {
		return nil, err
	}
	inits.Rdb.Set(inits.Ctx, "video", marshal, time.Minute*5)
	return &__.ListVideoResp{
		UserId:      int64(video.UserId),
		Title:       video.Title,
		Description: video.Description,
		CoverUrl:    video.CoverUrl,
		FileUrl:     video.FileUrl,
		Type:        video.Type,
		Address:     video.Address,
		Year:        video.Year,
		Play:        int64(video.Play),
		Like:        int64(video.Like),
		Collect:     int64(video.Collect),
		Comment:     int64(video.Comment),
		Status:      video.Status,
		Reject:      video.Reject,
	}, nil

}

func (s Server) VideoStatus(_ context.Context, in *__.VideoStatusReq) (*__.VideoStatusResp, error) {
	var video model.Video
	video = model.Video{
		Model: gorm.Model{
			ID: uint(in.VideoId),
		},
		Status: in.Status,
		Reject: in.Reject,
	}
	err := model.UpdateVideo(&video, int(video.ID))
	if err != nil {
		return &__.VideoStatusResp{
			Code: 0,
			Msg:  "审核失败",
		}, nil
	}
	return &__.VideoStatusResp{
		Code: 200,
		Msg:  "审核成功",
		Data: int64(video.ID),
	}, nil
}

func (s Server) CreateVideo(_ context.Context, in *__.CreateVideoReq) (*__.CreateVideoResp, error) {
	//TODO implement me
	var video model.Video
	video = model.Video{
		UserId:      int(in.UserId),
		Title:       in.Title,
		Description: in.Description,
		CoverUrl:    in.CoverUrl,
		FileUrl:     in.FileUrl,
		Type:        in.Type,
		Address:     in.Address,
		Year:        in.Year,
		Play:        int(in.Play),
		Like:        int(in.Like),
		Collect:     int(in.Collect),
		Comment:     int(in.Comment),
		Status:      in.Status,
	}
	err := model.CreateVideo(&video)
	if err != nil {
		return &__.CreateVideoResp{
			Code: 0,
			Msg:  "发布失败",
		}, nil
	}
	return &__.CreateVideoResp{
		Code: 200,
		Msg:  "发布成功",
		Data: int64(video.ID),
	}, nil
}
